package com.codeoss.module.sale.pagebean;

import java.math.BigDecimal;

import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import com.codeoss.framework.context.YunContext;
import com.codeoss.framework.view.pagebean.mobile.MDetailPageBean;
import com.codeoss.framework.view.service.IVariableService;
import com.codeoss.module.account.model.AccountTax;
import com.codeoss.module.account.service.IAccountTaxService;
import com.codeoss.module.product.model.ProductProduct;
import com.codeoss.module.product.service.IProductProductService;
import com.codeoss.module.uom.service.IUomUomConversionService;

@Service("mSaleQuotationLineDetailPageBean")
public class MSaleQuotationLineDetailPageBean extends MDetailPageBean{
	@Autowired
	private IVariableService variableService;
	@Autowired
    private IUomUomConversionService uomUomConversionService;
	@Autowired
    private IAccountTaxService accountTaxService;
	@Autowired
    private IProductProductService productProductService;
	public void onProductChangeHandler() {
		YunContext yunContext=YunContext.getCurrentContext();
		Long productId=variableService.getVariableValue(yunContext,"sale_quotation_line_product_product_id");
		ProductProduct productProduct=productProductService.getById(productId);
		BigDecimal salePrice=productProduct.getSalePrice();
		variableService.setVariableValue(yunContext, "sale_quotation_line_price_entered", salePrice);
		Long uomUomId=productProduct.getUomUomId();
		variableService.setVariableValue(yunContext, "sale_quotation_line_uom_uom_id", uomUomId);
	}
	
	public BigDecimal calculateLineTotal() {
		YunContext yunContext=YunContext.getCurrentContext();
		Long productId=variableService.getVariableValue(yunContext,"sale_quotation_line_product_product_id");
		BigDecimal qtyEntered=variableService.getVariableValue(yunContext,"sale_quotation_line_qty_entered");
		Long uomUomToId=variableService.getVariableValue(yunContext,"sale_quotation_line_uom_uom_id");
		BigDecimal priceEntered=variableService.getVariableValue(yunContext,"sale_quotation_line_price_entered");
		Long accountTaxId=variableService.getVariableValue(yunContext,"sale_quotation_line_account_tax_id");
		BigDecimal discount=variableService.getVariableValue(yunContext,"sale_quotation_line_discount");
		if(qtyEntered!=null &&uomUomToId!=null &&priceEntered!=null &&accountTaxId!=null) {
			BigDecimal qtyOrdered=uomUomConversionService.convert(productId, uomUomToId, qtyEntered);
			BigDecimal total=qtyOrdered.multiply(priceEntered);
			if(discount!=null) {
				total=total.multiply(new BigDecimal(100).subtract(discount)).divide(new BigDecimal(100));
			}
			AccountTax accountTax=accountTaxService.getById(accountTaxId);
			BigDecimal accountRate=(new BigDecimal(100)).add(accountTax.getRate()).divide(new BigDecimal(100));
			total=total.multiply(accountRate);
			//variableService.setVariableValue(yunContext, "sale_quotation_line_amount_subtotal", total);
			return total;
		}
		return null;
	}
	
}
